//
//  PlayerViewController.m
//  MP3Player
//
//  Created by kci yoo on 8/15/11.
//  Copyright 2011 Daum Communications. All rights reserved.
//

#import "PlayerViewController.h"

@implementation PlayerViewController

- (id)init
{
    self = [super init];
    if (self) {
        PlayerView * playerView ;
        playerView = [[[PlayerView alloc] initWithFrame:CGRectMake(0, 20, 320, 460)] autorelease];
        self.view = playerView;
        // Initialization code here.
        
        UIButton * btnPlay = [self.view btnPlay];
        [btnPlay addTarget:self action:@selector(onBtnPlayTapped:) forControlEvents:UIControlEventTouchUpInside];
        
        [[self.view btnPrev] addTarget:self action:@selector(onBtnPrevTapped:) forControlEvents:UIControlEventTouchDown];

        
        [[self.view btnNext] addTarget:self action:@selector(onBtnNextTapped:) forControlEvents:UIControlEventTouchDown];
        
        
        NSString * str = [[NSBundle mainBundle] pathForResource:@"secret" ofType:@"mp3"];
        NSURL * url = [NSURL fileURLWithPath:str];
        player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    }
    
    return self;
}

- (void) onBtnPrevTapped:(id *) btn
{
    NSLog(@"onBtnPrevTapped is Called");
}

- (void) onBtnPlayTapped:(id *) btn
{   
    
    UIButton * btnPlay = (UIButton *) btn;
    [btnPlay titleForState:0];
    NSLog(@"btnPlay %@",btnPlay);
    if(state == PLAYER_STATE_PLAYING)
    {
        //음악을 멈춘다. 
        state= PLAYER_STATE_STOPPED;
        [[self.view btnPlay] setTitle:@"재생" forState:0];
        [self stop];
    }
    else
    {
        // 음악을 다시 재생한다.
        state = PLAYER_STATE_PLAYING;
        [[self.view btnPlay] setTitle:@"정지" forState:0];
        [self play];

    }
    NSLog(@"onBtnPlayTapped");
}

- (void)play
{
    [player play];
    
    timer = [[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(onUpdated:) userInfo:nil repeats:YES] retain];
}

- (void)onUpdated:(NSTimer *)timer
{
    NSLog(@"timer is called");
    [[self.view progressBar] setValue:[player currentTime] / [player duration]];
    
    /*
    [[self.view lblCurrentTime] setText:[NSString stringWithFormat:@"%d", [player currentTime]]];
    [[self.view lblLeftTime] setText:[NSString stringWithFormat:@"%d", [player duration] - [player currentTime]]];
     */
}

- (void)stop
{
    [timer invalidate];
    timer = nil;
    [player pause];
}

- (void) onBtnNextTapped:(id *) btn
{
    UIAlertView * alertView;
    alertView = [[UIAlertView alloc] initWithTitle:@"알림" message:@"다음곡 재생" delegate:self cancelButtonTitle:@"취소" otherButtonTitles:@"확인" , nil];
    [alertView show];
    [alertView release];
    
    
    
    NSLog(@"onBtnNextTapped");
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    if(buttonIndex==1)
    {
        ModalViewController * mvc = [[ModalViewController alloc] init];
         [self presentModalViewController:mvc animated:YES];
        mvc.delegate = self;
        [mvc release];
    }
    else if(buttonIndex ==0)
    {
        ModalViewController * mvc = [[ModalViewController alloc] init];
        mvc.delegate = self;
         
        [self presentModalViewController:mvc animated:YES];
        
        [mvc release];
    }
}

- (void)modalViewController:(ModalViewController *)modalViewController didFinishWithObject:(id *)object
{
    [self dismissModalViewControllerAnimated:YES];
}

- (void)dealloc {
    [player release];
    [super dealloc];
}

@end
